<?php
/**
 * Script file
 * @author Serguei Shimansky <privateteacher@mail.ru>
 * @copyright Copyright (c) 2011, Serguei Shimansky
 * @version 1.0
 * @package fast-comment
 */

require_once '../lib/lib_global2.php';
require_once '../inc/vars2.php';
include '../inc/regional.php';

//russian strings printable - change them to whatever you wish
$s_leave_a_comment = 'Leave a comment';
$s_today = 'Today';
$s_writes_at = 'writes at';

$r = '';

$p = '';

$limit = lib_global2_get_post('limit');
if (!$limit) {$limit = 20;}

$load = lib_global2_get_post('load');

if (!empty($load)) {


	$mysql_connect = @mysql_connect($pt_regional['mysql_db_host'], $pt_regional['mysql_db_user'], $pt_regional['mysql_db_pass']) or die(@mysql_error());
	$mysql_select_db = @mysql_select_db($pt_regional['mysql_db_name']) or die(@mysql_error());
	@mysql_query("SET NAMES 'utf8'");

    if (lib_global2_mysql_table_exists($pt_guestbook_demo_table_name)) {
    	$query = @mysql_query("SELECT `id`, `adddate`, `user_login`, `msg`, `user_host`, `user_ip` FROM `" . $pt_guestbook_demo_table_name . "` WHERE `msg`!='' AND `adddate`!='' ORDER BY `adddate` DESC LIMIT " . $limit . ";");

    	if (@mysql_num_rows($query) > 0) {

    		$r = 1;

    		while ($fr = @mysql_fetch_row($query)) {

    			//$date_str = lib_global2_to_rudate($fr[1]);//this will convert date to russian lamguage
                $date_str = date('l jS \of F Y', $fr[1]);
                if ($vars2_start_time < $fr[1] &&  $vars2_end_time > $fr[1]) {$date_str = $s_today;}

    			$fr[3] = lib_global2_prepare_str($fr[3]);
    			$fr[3] = lib_global2_ord_space($fr[3]);
    			$fr[3] = lib_global2_remove_bbcoded($fr[3]);
    			$fr[3] = lib_global2_make_clickable_links(lib_global2_ensure_lt_gt(lib_global2_ensure_amp($fr[3])));

    			$p .= '<div><div style="font-size:smaller;font-style:italic;"><span style="font-size:larger;">' . $fr[5] . '</span> ' . $s_writes_at . ' <span style="font-size:larger;">' . date("H:i:s", $fr[1]) . '</span><br />
' . $date_str . ':</div><div>&#160;</div><div><blockquote>&#8212; ' . $fr[3] . '</blockquote></div><div>&#160;</div></div>' . "\n";
    		}
    	}
    } else {
        $p = 'Connected to DB successfully. Now create table from dump file pt_guestbook_demo.sql in DB, its name has to be stated in regional.php file.';
    }
}

if ($load == 'posts') {
	if ($r) {
		echo '<p><a href="#" onclick="showSimplemodalOverlayClose(\'simplemodal_guestbook\');obj=document.getElementById(\'div_guestbook_check\');while(obj.firstChild) obj.removeChild(obj.firstChild);">' . $s_leave_a_comment . '</a></p>' . "\n" . '<div>' . "\n" . preg_replace("/\<div\>\&\#160\;\<\/div\>\<\/div\>$/i","</div>",$p) . "\n" . '</div>' . "\n";
		exit;
	} else {
	    echo $p;
	}
}
